package com.tulin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class MyWebSecurityConfig  {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
//                .csrf().disable()//关闭csrg跨域检查
                 .authorizeRequests(authorizeRequests ->authorizeRequests
                                                .requestMatchers("/mobile/**").hasAuthority("mobile") // 配置资源权限
                                                .requestMatchers("/salary/**").hasAuthority("salary")
                                                .requestMatchers("/common/**").permitAll() // common下的请求直接通过
                                                .anyRequest().authenticated() // 其他请求需要登录
                                )
                                .formLogin(formLogin ->
                                        formLogin
                                                .loginPage("/login") // 使用默认的登录页面
                                                .defaultSuccessUrl("/main.html") // 登录成功后跳转到main.html
//                                                .failureUrl("/common/loginFailed") // 登录失败后跳转到/common/loginFailed
                                );

        return http.build();
    }
}
